package cn.hinglo.gameclaw.admin.action;

import cn.hinglo.gameclaw.admin.service.HtmlService;
import cn.hinglo.gameclaw.data.controller.BaseController;
import cn.hinglo.gameclaw.data.entity.Result;
import cn.hinglo.gameclaw.user.bean.Html;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;

/**
 * @createTime: 2018/1/3
 * @author: HingLo
 * @description: html 后台管理
 */
@RestController
@RequestMapping("admin/h/")
@Slf4j
public class HtmlAction extends BaseController {
    private final HtmlService htmlService;

    @Autowired
    public HtmlAction(HtmlService htmlService) {
        this.htmlService = htmlService;
    }


    /**
     * 查看全部的html 信息
     *
     * @return HTML列表
     */
    @GetMapping("list")
    public Result list() {
        return htmlService.listService();
    }

    /**
     * 添加html
     *
     * @param html          HTML对象
     * @param bindingResult 错误结果对象
     * @return 返回添加结果
     */
    @PostMapping("add")
    public Result addHtml(@Valid Html html, BindingResult bindingResult) {
        if (bindingResult.hasErrors()) {
            return error(bindingResult.getFieldError().getDefaultMessage());
        }
        return htmlService.addHtmlService(html);
    }


    /**
     * 根据ID来删除HTML
     *
     * @param id HTML的唯一Id
     * @return 删除结果
     */
    @DeleteMapping("delete")
    public Result deleteHtml(String id) {
        return htmlService.deleteHtmlService(id);
    }

}
